import cn.itcast.dao.UserDao;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import itchas.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class 多页测试 {

    @Test
    public void testPage()throws Exception{
        /*1.读取配置文件并创建SqlSessionFactory*/
        InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        /*2.获取sqlSession[这是mybatis的重要API, 使用它可以操作数据库crud*/
        SqlSession sqlSession = sqlSessionFactory.openSession();

        /*3.查询第一页的数据，每页显示2条*/
        PageHelper.startPage(1,2);

        /*4.由Mybatis框架创建UserDao的实现类对象，我们称为代理对象*/
        UserDao userDao = sqlSession.getMapper(UserDao.class);

        /*5.创建查询条件并调用接口的条件查询方法*/

        List<User> users = userDao.findAll();
        System.out.println(users);

        /*6.释放资源*/
        sqlSession.close();

        //测试查询结果
        PageInfo<User> pi = new PageInfo<>(users);
        System.out.println("总页数:"+pi.getPages());
        System.out.println("总记录数:"+pi.getTotal());
        System.out.println("每页显示条数:"+pi.getPageSize());
        System.out.println("当前第:"+pi.getPageNum()+"页");

    }
}
